package com.app.news;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.app.news.util.DB;


public class IndexActivity extends AppCompatActivity {
    private EditText usernameEd;
    private EditText passwordEd;
    private Button loginBtn;
    private CheckBox remember;
    private TextView signInTv;
    private String usernameStr;
    private String passwordStr;
    private SharedPreferences sp;
    private TextView aboutTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_index);
        initView();
        initEvent();
        sp=this.getSharedPreferences("config",0);
        String usernameed=sp.getString("usernameStr",usernameStr);
        String passworded=sp.getString("passwordStr",passwordStr);
        usernameEd.setText(usernameed);
        passwordEd.setText(passworded);
    }
    private void initView(){
        usernameEd = findViewById(R.id.username_et);
        passwordEd = findViewById(R.id.password_et);
        loginBtn = findViewById(R.id.login_btn);
        remember = findViewById(R.id.remember_cb);
        signInTv = findViewById(R.id.sign_in_tv);
        aboutTv = findViewById(R.id.setting_tv);
    }
    private void initData(){
        usernameStr = usernameEd.getText().toString()+"";
        passwordStr = passwordEd.getText().toString()+"";
        if (remember.isChecked()){
            SharedPreferences.Editor editor=sp.edit();
            editor.putString("usernameStr",usernameStr);
            editor.putString("passwordStr",passwordStr);
            editor.commit();
        }
    }
    private void initEvent(){







        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();
                DB db=new DB(IndexActivity.this);
                Boolean result=db.loginSelect(usernameStr,passwordStr);
                if (result){
                    Intent intent=new Intent(IndexActivity.this,TwoActivity.class);
                    startActivity(intent);
                    Toast.makeText(IndexActivity.this,"登录成功,请连接网络",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(IndexActivity.this,"登录失败",Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent(IndexActivity.this,Main3Activity.class);
                    startActivity(intent);
                }
            }
        });
        signInTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
        aboutTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(IndexActivity.this,AboutActivity.class);
                startActivity(intent);
            }
        });
    }
}
